package com.heima.news.rabbit;

import com.heima.news.config.RabbitMQConfig;
import com.heima.news.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class DelayNewsListener {

    @Autowired
    private NewsService newsService;

    @RabbitListener(queues = RabbitMQConfig.DELAY_QUEUE)
    public void handleDelayNews(Long newsId) {
        log.info("【MQ接收成功】接收到延迟发布消息，新闻ID={}", newsId);
        try {
            newsService.publishNewsById(newsId);
            log.info("【新闻发布成功】新闻ID={}", newsId);
        } catch (Exception e) {
            log.error("【发布失败】新闻ID={}，原因：{}", newsId, e.getMessage(), e);
        }
    }
}
